package test.panel4;

import test.CPU.CPUController;
import test.panel3.ProcessTableInPanel;

import java.util.HashMap;
import java.util.Map;

public class Process {
    static int nextPid = 1;
    public int pid;
    public String name;
    public boolean isRunning;
    public int memory = 10; // 所需内存
    public Map<Integer, Integer> Needs = new HashMap<>(); // 资源ID -> 需要的资源数
    public Map<Integer, Integer> Allocation = new HashMap<>(); // 资源ID -> 已分配的资源数
    public int needTime = 10;//完成所需时间
    public int workedTime = 0;//已工作时间
    public int period = 20;//周期
    public String state = "挂起";//状态：运行，阻塞，挂起，完成,等待

    public Boolean resourceFlag = false;//表示资源有无分配

    public Process(String name, boolean isRunning) {
        pid = nextPid++;
        this.name = name;
        this.isRunning = isRunning;
    }

    public Process(String name, boolean isRunning, Map<Integer, Integer> needs) {
        pid = nextPid++;
        this.name = name;
        this.isRunning = isRunning;
        this.Needs = needs;
        this.Allocation = new HashMap<>();
        // 初始化Allocation为0
        for (Integer resourceId : needs.keySet()) {
            Allocation.put(resourceId, 0);
        }
    }

    // 获取资源需求
    public Map<Integer, Integer> getNeeds() {
        return Needs;
    }

    // 获取已分配的资源
    public Map<Integer, Integer> getAllocation() {
        return Allocation;
    }

    // 分配资源
    public void allocateResources(Map<Integer, Integer> allocation) {
        for (Map.Entry<Integer, Integer> entry : allocation.entrySet()) {
            int resourceId = entry.getKey();
            int amount = entry.getValue();
            Allocation.put(resourceId, Allocation.getOrDefault(resourceId, 0) + amount);
        }
    }

    //设置状态
    public void setState(String state) {
        this.state = state;
    }

    //得到状态
    public String getState() {
        return state;
    }


    // 清空资源分配记录
    public void clearAllocation() {
        this.Allocation.clear();
    }

    @Override
    public String toString() {
        int percent = workedTime * 100 / needTime;
        return "pid:"
                .concat(String.valueOf(pid))
                .concat("  name:")
                .concat(name)
                .concat("  状态：")
                .concat(state)
                .concat("  进度：")
                .concat(String.valueOf(percent) + '%');
    }

    public void run() {
        if (!resourceFlag) {
            if (ResourceGUI.getInstance().checkAndAllocateResources(this)) resourceFlag = true;//分配资源
            else {
                state = "资源不足";
                Scheduler.getInstance().CPUUpdate(CPUController.getInstance().getCpuClock());//查找一个新进程
                return;
            }
        }
        ProcessTableInPanel.getInstance().addData(this);
        if (workedTime >= needTime) {
            state = "完成";
            //释放资源
            ResourceGUI.getInstance().releaseResources(this);
            this.resourceFlag = false;
            return;
        }
        state = "运行";
        workedTime++;
        if (workedTime >= needTime) state = "完成";
        //释放资源
        if (resourceFlag) {
            resourceFlag = false;
            ResourceGUI.getInstance().releaseResources(this);
        }
        this.resourceFlag = false;
    }
}
